<?php

namespace app\admin\logic;


use app\common\model\mysql\ProjectTopicContentModel;
use app\common\model\mysql\ProjectTopicModel;
use app\common\logic\BaseLogic;
use think\facade\Db;

class ProjectTopicLogic extends BaseLogic
{
    public function index($request)
    {
        $search = $request->param();

        $where = [['project_id', '=', $search['project_id']]];
        if (!empty($search['name'])) {
            $where[] = ['name', 'like', "%{$search['name']}%"];
        }

        $list = ProjectTopicModel::getList($where, '*', [], ['sort' => 'asc']);

        return view('ProjectTopic/topic', compact('list', 'search'));
    }

    public function create($request)
    {
        $param = $request->param();

        return view('ProjectTopic/topic_add', compact('param'));
    }

    public function save($request)
    {
        $param = $request->param();

        $inputData = [
            'project_id' => $param['project_id'],
            'title' => $param['title'],
            'subject' => $param['subject'],
            'type' => $param['type'],
            'sort' => $param['sort'],
            'status' => $param['status'],
            'num' => $param['num'],
        ];

        Db::startTrans();
        try {
            $res = ProjectTopicModel::addData($inputData);

            $real = explode(',', $param['real']);
            $ids = [];
            foreach ($param['content'] as $value) {
                if (!$value) continue;
                $data = [
                    'project_id' => $param['project_id'],
                    'topic_id' => $res['id'],
                    'content' => $value,
                ];
                ProjectTopicContentModel::addData($data);

                if (in_array($value, $real)) {
                    $ids[] = $value;
                }
            }

            if ($ids) {
                ProjectTopicModel::editData(['content_ids' => implode(',', $ids)], ['id' => $res['id']]);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }

    public function edit($request, $id)
    {
        $info = ProjectTopicModel::getOne(['id' => $id]);

        $num = count($info->content);

        return view('ProjectTopic/topic_edit', compact('info', 'num'));
    }

    public function update($request, $id)
    {
        $param = $request->param();

        $inputData = [
            'title' => $param['title'],
            'subject' => $param['subject'],
            'type' => $param['type'],
            'sort' => $param['sort'],
            'status' => $param['status'],
            'num' => $param['num'],
        ];

        Db::startTrans();
        try {
            ProjectTopicModel::editData($inputData, ['id' => $id]);

            $real = explode(',', $param['real']);
            $ids = [];
            $contentIds = [];
            foreach ($param['content'] as $key => $value) {


                if (!$value) continue;
                $data = [
                    'project_id' => $param['project_id'],
                    'topic_id' => $id,
                    'content' => $value,
                ];
                if (ProjectTopicContentModel::getAlone(['id' => $key, 'topic_id' => $id])) {
                    ProjectTopicContentModel::editData($data, ['id' => $key]);
                    $content['id'] = $key;
                } else {
                    $content = ProjectTopicContentModel::addData($data);

                }

                $contentIds[] = $content['id'];
                if (in_array($value, $real)) {
                    $ids[] = $value;
                }
            }

            if ($ids) {
                ProjectTopicModel::editData(['content_ids' => implode(',', $ids)], ['id' => $id]);
            }

            if ($contentIds) {
                ProjectTopicContentModel::deleteData([['id', 'not in', $contentIds], ['topic_id', '=', $id]], true);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $this->responseError('保存失败');
        }


        return $this->responseSuccess('保存成功');
    }


    public function renew($request, $id)
    {
        $param = $request->param();
        $inputData = [];
        isset($param['status']) ? $inputData['status'] = $param['status'] : false;
        isset($param['sort']) ? $inputData['sort'] = $param['sort'] : false;
        try {
            ProjectVideoModel::editData($inputData, ['id' => $id]);
        } catch (\Exception $e) {
            return $this->responseError('保存失败');
        }
        return $this->responseSuccess('保存成功');
    }
}